// 24.两两交换链表中的结点
package LeetcodeHot100;


public class Solution024 {
    public ListNode swapPairs(ListNode head) {
        ListNode pre = new ListNode(0);
        ListNode result = pre;
        while (head != null) {
            if (head.next != null) {
                ListNode node = head.next.next;
                head.next.next = pre.next;
                pre.next = head.next;
                pre = pre.next;
                head.next = pre.next;
                pre.next = head;
                pre = pre.next;
                head = node;
            } else {
                head.next = pre.next;
                pre.next = head;
                pre = pre.next;
                head = head.next;
            }
        }
        return result.next;
    }
}
